package com.vanke.devops.api.base;

import com.baomidou.mybatisplus.plugins.Page;
import com.vanke.core.bean.ApiBaseResponse;
import com.vanke.core.bean.PagingResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author Youzhijie
 * @since 2017-12-05
 */
public class BaseController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    protected ApiBaseResponse setResponse(){
        return new ApiBaseResponse();
    }

    protected ApiBaseResponse setResponse(Object data){
        if (data != null) {
            if (data instanceof Page) {
                PagingResponse response = new PagingResponse<>();
                Page<?> page = (Page<?>) data;
                response.setData(page.getRecords());
                response.setTotal(page.getTotal());
                return response;
            } else {
                ApiBaseResponse response = new ApiBaseResponse();
                response.setData(data);
                return response;
            }
        }
        return new ApiBaseResponse();
    }

    protected <T>PagingResponse setResponse(List<T> rows, int total){
        return new PagingResponse<>(rows,total);
    }
    protected <T>PagingResponse setResponse(Page<T> page){
        return new PagingResponse<>(page.getRecords(),page.getTotal());
    }
}

